﻿<cfsilent>
	<cfscript>
		
		event.setArg("pageTitle", "选课管理");
		
		datetimeAdvice = getProperty("serviceFactory").getBean("datetimeAdvice");
		propAdvice = getProperty("serviceFactory").getBean("turnPropertyAdvice");

		termId = event.getArg("TermID");

		turnId = event.getArg("CT");
		currentTab = event.getArg("TabID", "labSetting");

		rs_turnEntity = queryNew("ct");

		sql = "	SELECT
					ct.ct,
					ct.turn,
					ct.date_start,
					ct.date_end,
					ct.time_start,
					ct.time_end,
					ct.turn_prop
				FROM
					t_choose_turn ct 
				WHERE ct.ct = '1' 
				ORDER BY
					ct.ct ";
		
		queryObj = new Query(datasource = application.dnsSlave);
		rs_turn = queryObj.execute(sql = sql).getResult();
		
		if ( len(turnId) ) {

			sql = "	SELECT
						ct.turn,ct.ct,ct.date_start,ct.date_end,ct.time_start,ct.time_end,ct.grade_allowed,
						ct.institute_allowed,ct.subject_allowed
					FROM
						t_choose_turn ct
					WHERE
						ct.ct = :turnId ";
			
			queryObj = new Query(datasource = application.dnsSlave);
			queryObj.addParam(name = "turnId", value = turnId, cfsqltype = "cf_sql_char");
			rs_turnEntity = queryObj.execute(sql = sql).getResult();
			
			if( rs_turnEntity.recordCount ){
				currentTab = "labEdit";
			}
			
			/* 读取选课学年的授课年级、学院信息 */
			sql = "	SELECT
						DISTINCT 
						a.grade,
						i.institute_id,i.institute_short
					FROM
						t_class a
						INNER JOIN t_subject sbj ON sbj.sbj_id = a.sbj_id
						INNER JOIN t_institute i ON sbj.institute_id = i.institute_id
					WHERE
						a.cls_scale > 0
						AND
						EXISTS(
							SELECT 
							    1 
							FROM 
							    t_plan b 
							WHERE 
							    a.cls_id = b.cls_id AND b.term_id = :termId  AND b.cidx != 'XXX' AND b.cidx != '000'
						)
					ORDER BY
						a.grade DESC,i.institute_short";
			
			queryObj = new Query(datasource = application.dnsSlave);
			queryObj.addParam(name = "termId", value = termId, cfsqltype = "cf_sql_char");
			rs_subject = queryObj.execute(sql = sql).getResult();
			
			gradeArray = [];
			insArray = [];

			for(a = 1; a lte rs_subject.recordCount; a++){
				if (not arrayFind(gradeArray, rs_subject['grade'][a])) { arrayAppend(gradeArray, rs_subject['grade'][a]); }
				if (not arrayFind(insArray, [rs_subject['institute_id'][a],rs_subject['institute_short'][a]])) { arrayAppend(insArray, [rs_subject['institute_id'][a],rs_subject['institute_short'][a]]); }
			}
		}

		/* 选课相关设置 */
		depDateStart = event.getArg("CHOOSE.DEP.DateStart");
		depDateEnd = event.getArg("CHOOSE.DEP.DateEnd");
		
		if ( depDateStart eq "" ) {
			depDateStart = dateFormat( now(), "YYYYMMDD" );
		}
		if ( depDateEnd eq "" ) {
			depDateEnd = dateFormat( dateAdd( "D", 14, now() ), "YYYYMMDD" );
		}

		passArgs = structNew();
		
	</cfscript>
</cfsilent>

<div id="headArea">
	<div class="uiHeader uiHeaderWithImage ptm">
		<div class="clearfix uiHeaderTop">
			<div class="uiHeaderActions rfloat">
				<a class="uiButton" href="<cfoutput>#buildURL('taskClass')#</cfoutput>"><i class="mrs img btnPublish"></i><span class="uiButtonText">教学任务</span></a>
			</div>
			<div>
				<h2 class="uiHeaderTitle">
					<i class="uiHeaderImage img icon16x16 setting"></i>选课时间段设置
				</h2>
			</div>
		</div>
	</div>
</div>

<div class="UIElement">
	<ul class="senateFirstPage">
		<li>
			<div class="clearfix" id="create">
				<div class="UItab">
					<div class="tabLables">
						<span <cfif currentTab eq "labSetting">class="active"</cfif> id="labSetting" tabTarget="Setting">选课轮次</span>
						<!---<span <cfif currentTab eq "labCondition">class="active"</cfif> id="labCondition" tabTarget="Condition">选课限制条件</span>--->
                        <cfif rs_turnEntity.recordCount>
							<span <cfif currentTab eq "labEdit">class="active"</cfif> id="labEdit" tabTarget="Edit">修改<cfoutput>#rs_turnEntity.turn#</cfoutput>轮次安排</span>
						</cfif>
					</div>
				</div>

				<script language="javascript" type="text/javascript">
					//<![CDATA[
					<!--
					$(document).ready(function() {
						
						/* init Tab */
						$("div.tabContent").hide();
						$("div#" + $("div.tabLables span.active").attr("tabTarget")).show();
						
						$("div.tabLables > span").click(function(){
							var $targetID = $(this).attr("tabTarget");
							
							$("div.tabLables span").removeClass("active");
							$(this).addClass("active");
							
							$("div.tabContent").not('[id="' + $targetID + '"]').hide();
							$("div.tabContent#" + $targetID).show();
						});
						
					});
					
					// -->
					//]]>
				</script>
				
				<div id="Setting" class="tabContent">
					<div class="noticeBlock">
						<h3>选课时间段安排</h3>
						<hr/>
					</div>
					
					<table id="turnList" class="UIEditable">
						<thead>
							<tr>
								<td width="60"></td>
								<td width="160">选课日期</td>
								<td>选课时间</td>
								<td width="30">启用</td>
								<td width="30">计划</td>
								<!---<td width="30">通选</td>
								<td width="30">体育</td>
								<td width="30">重修</td>
								<td width="30">补修</td>
								<td width="30">辅修</td>
								<td width="36">双学位</td>
                                <td width="55">提前毕业</td>--->
								<td width="18"></td>
							</tr>
						</thead>
						<tbody>
                            <cfloop query="rs_turn">
								
								<cfset structInsert(passArgs, "CT", rs_turn.ct, true) />
								<cfset propAdvice.parseProperty( rs_turn.turn_prop ) />
								
								<tr class="editRows" rowid="<cfoutput>#rs_turn.ct#</cfoutput>">
									<td><strong><cfoutput>#rs_turn.turn#</cfoutput></strong></td>
									<td><span class="numeric"><cfoutput>#datetimeAdvice.formatDateString(rs_turn.date_start, 'YYYY/MM/DD')#</cfoutput> - <cfoutput>#datetimeAdvice.formatDateString(rs_turn.date_end, 'YYYY/MM/DD')#</cfoutput></span></td>
									<td>
										<cfif rs_turn.time_start eq "0001" and rs_turn.time_end eq "2359">
											全天
											<cfelse>
											<span class="numeric"><cfoutput>#datetimeAdvice.formatTimeString(rs_turn.time_start, 'HH:MI')#</cfoutput> ~ <cfoutput>#datetimeAdvice.formatTimeString(rs_turn.time_end, 'HH:MI')#</cfoutput></span>	
										</cfif>
									</td>
									
									<td><a usage="Running" rowid="<cfoutput>#rs_turn.ct#</cfoutput>" href="javascript:void(0);" class="stat <cfif propAdvice.getRunning()>yes<cfelse>no</cfif>"></a></td>
									<td><a usage="ProgramTask" rowid="<cfoutput>#rs_turn.ct#</cfoutput>" href="javascript:void(0);" class="stat <cfif propAdvice.getProgramTask()>yes<cfelse>no</cfif>"></a></td>
									<!---<td><a usage="CommonTask" rowid="<cfoutput>#rs_turn.ct#</cfoutput>" href="javascript:void(0);" class="stat <cfif propAdvice.getCommonTask()>yes<cfelse>no</cfif>"></a></td>
                                    <td><a usage="Sport" rowid="<cfoutput>#rs_turn.ct#</cfoutput>" href="javascript:void(0);" class="stat <cfif propAdvice.getSport()>yes<cfelse>no</cfif>"></a></td>
									<td><a usage="Retaking" rowid="<cfoutput>#rs_turn.ct#</cfoutput>" href="javascript:void(0);" class="stat <cfif propAdvice.getRetaking()>yes<cfelse>no</cfif>"></a></td>
									<td><a usage="FixUp" rowid="<cfoutput>#rs_turn.ct#</cfoutput>" href="javascript:void(0);" class="stat <cfif propAdvice.getFixUp()>yes<cfelse>no</cfif>"></a></td>
									<td><a usage="Assist" rowid="<cfoutput>#rs_turn.ct#</cfoutput>" href="javascript:void(0);" class="stat <cfif propAdvice.getAssist()>yes<cfelse>no</cfif>"></a></td>
									<td><a usage="Secondary" rowid="<cfoutput>#rs_turn.ct#</cfoutput>" href="javascript:void(0);" class="stat <cfif propAdvice.getSecondary()>yes<cfelse>no</cfif>"></a></td>
                                    <td><a usage="EarlyGraduate" rowid="<cfoutput>#rs_turn.ct#</cfoutput>" href="javascript:void(0);" class="stat <cfif propAdvice.getEarlyGraduate()>yes<cfelse>no</cfif>"></a></td>--->
                                    
									<td><a href="<cfoutput>#buildURL('DEFAULT_EVENT', passArgs)#</cfoutput>" class="operation edit"></a></td>
								</tr>
								
							</cfloop>
						</tbody>
					</table>
					<script language="javascript" type="text/javascript">
						//<![CDATA[
						<!--
						$(document).ready(function() {
							
							/* turn usage trigger */
							$("a[usage]", $("#turnList")).click(function(){
								
								var rowid = $(this).attr("rowid");
								var method = $(this).attr("usage");

								var status = $(this).hasClass("yes");

								var newStatus = true;
								
								var $row = $("tr.editRows[rowid='" + rowid + "']", $("#turnList"));
								
								/* 切换状态数据 */
								if (status) {
									newStatus = false;
								}
								
								/* 发送请求 */
								$(this).removeClass("no").removeClass("yes").addClass("waiting");
								
								$.post(
										"<cfoutput>#buildURL('apiChooseTurnPropChange')#</cfoutput>", 
										{
											'ct' : rowid,
											'prop' : method,
											'status' : newStatus,
											'seed': (new Date()).getTime()
										}, 
										function(data) {
											
											//alert(data);
											result = $.parseJSON(data);
											
											$(this).removeClass("waiting");
											
											/* 刷新允许选课方式数据 */
											if (result.Running) { $("a[usage='Running']", $row).removeClass("no").addClass("yes"); } else { $("a[usage='Running']", $row).removeClass("yes").addClass("no"); }

											if (result.ProgramTask) { $("a[usage='ProgramTask']", $row).removeClass("no").addClass("yes"); } else { $("a[usage='ProgramTask']", $row).removeClass("yes").addClass("no"); }
											if (result.CommonTask) { $("a[usage='CommonTask']", $row).removeClass("no").addClass("yes"); } else { $("a[usage='CommonTask']", $row).removeClass("yes").addClass("no"); }
											if (result.Retaking) { $("a[usage='Retaking']", $row).removeClass("no").addClass("yes"); } else { $("a[usage='Retaking']", $row).removeClass("yes").addClass("no"); }
											if (result.Assist) { $("a[usage='Assist']", $row).removeClass("no").addClass("yes"); } else { $("a[usage='Assist']", $row).removeClass("yes").addClass("no"); }
											if (result.Secondary) { $("a[usage='Secondary']", $row).removeClass("no").addClass("yes"); } else { $("a[usage='Secondary']", $row).removeClass("yes").addClass("no"); }
											if (result.FixUp) { $("a[usage='FixUp']", $row).removeClass("no").addClass("yes"); } else { $("a[usage='FixUp']", $row).removeClass("yes").addClass("no"); }
											if (result.EarlyGraduate) { $("a[usage='EarlyGraduate']", $row).removeClass("no").addClass("yes"); } else { $("a[usage='EarlyGraduate']", $row).removeClass("yes").addClass("no"); }
											if (result.Sport) { $("a[usage='Sport']", $row).removeClass("no").addClass("yes"); } else { $("a[usage='Sport']", $row).removeClass("yes").addClass("no"); }
											
										
										});								
							});
							
						});
						// -->
						//]]>
					</script>
					
				</div>
				<cfif rs_turnEntity.recordCount>
				
					<div id="Edit" class="tabContent">
					
						<div class="noticeBlock">
							<h3><cfoutput>#rs_turnEntity.turn#</cfoutput>轮次相关设置</h3>
							<hr/>
						</div>
						
						<form id="formEdit" onSubmit="javascript:return processVerfiyForm('formEdit');" class="formWrapper" method="post" action="<cfoutput>#buildURL('chooseTurnUpdateDo')#</cfoutput>">
						
							<input type="hidden" id="TurnID" name="TurnID" value="<cfoutput>#rs_turnEntity.ct#</cfoutput>" />
							
							<div class="label">
								<span class="req">*</span><b>开始日期</b>
								<input name="DateStart" id="DateStart" type="text" class="typeDate" verification="date" maxlength="10" value="<cfoutput>#datetimeAdvice.formatDateString(rs_turnEntity.date_start)#</cfoutput>" /><span id="startTrigger" class="btnCalendar"></span>
							</div>
							<div class="label">
								<span class="req">*</span><b>结束日期</b>
								<input name="DateEnd" id="DateEnd" type="text" class="typeDate" verification="date" maxlength="10" value="<cfoutput>#datetimeAdvice.formatDateString(rs_turnEntity.date_end)#</cfoutput>" /><span id="endTrigger" class="btnCalendar"></span>
							</div>
							
							<div class="label">
								<span class="req">*</span><b>网上选课开放时间段</b>
								<input name="FullDay" id="FullDay" type="checkbox" class="typeCheckbox" value="1" <cfif rs_turnEntity.time_start eq "0001" and rs_turnEntity.time_end eq "2359">checked="checked"</cfif>/>全天
								<div id="notFullDay" class="<cfif rs_turnEntity.time_start eq "0001" and rs_turnEntity.time_end eq "2359">hidden_elem</cfif>">
									或 指定时间
									<input name="TimeStart" id="TimeStart" type="text" class="typeTime" verification="time" maxlength="5" value="<cfoutput>#datetimeAdvice.formatTimeString(rs_turnEntity.time_start, 'HH:MI')#</cfoutput>" />
									至
									<input name="TimeEnd" id="TimeEnd" type="text" class="typeTime" verification="time" maxlength="5" value="<cfoutput>#datetimeAdvice.formatTimeString(rs_turnEntity.time_end, 'HH:MI')#</cfoutput>" />
								</div>
							</div>
							
							<div class="label">
								<b>允许选课年级</b>
								<input name="FullGrade" id="FullGrade" type="checkbox" class="typeCheckbox" value="1" <cfif rs_turnEntity.grade_allowed eq "ALL">checked="checked"</cfif>/>所有年级
								<div id="notFullGrade" class="secondRow <cfif rs_turnEntity.grade_allowed eq "ALL">hidden_elem</cfif>">
									或 <br/> 指定年级
									<cfloop array="#gradeArray#" index="grade">
										<input name="targetGrade" type="checkbox" class="typeCheckbox" verification="group" value="<cfoutput>#grade#</cfoutput>" <cfif rs_turnEntity.grade_allowed eq "ALL" or listFind(rs_turnEntity.grade_allowed, grade, ",")>checked="checked"</cfif> /><cfoutput>#grade#</cfoutput>级
									</cfloop>
								</div>
							</div>
							
							<div class="label"> 
								<b>允许选课学院</b>
								<input name="FullInstitute" id="FullInstitute" type="checkbox" class="typeCheckbox" value="1" <cfif rs_turnEntity.institute_allowed eq "ALL">checked="checked"</cfif>/>
								所有学院
								<div id="notFullInstitute" class="secondRow <cfif rs_turnEntity.institute_allowed eq "ALL">hidden_elem</cfif>"> 
									
									或 <br/> 指定学院
									<table>
										<tr>
										<cfset row = 0 />
										<cfset cellPerRow = 5 />
										<cfloop array="#insArray#" index="dep">
											<cfset row++ />
												<td>
													<input name="targetInstitute" usage="targetInstitute" type="checkbox" class="typeCheckbox" value="<cfoutput>#dep[1]#</cfoutput>" <cfif rs_turnEntity.institute_allowed eq "ALL" or listFind(rs_turnEntity.institute_allowed, dep[1], ",")>checked="checked"</cfif> />
													<cfoutput>#dep[2]#</cfoutput>
												</td>
											<cfif row mod cellPerRow eq 0>
											</tr>
											<tr>
											</cfif>
										</cfloop>
										<cfif row eq arrayLen(insArray) and arrayLen(insArray) mod cellPerRow>
											<cfloop from="1" to="#cellPerRow-arrayLen(insArray) mod cellPerRow#" index="i">
												<td><!-- <cfoutput>holder<cfoutput>#i#</cfoutput> --></cfoutput></td>
											</cfloop>
										</cfif>
										</tr>
									</table>
									
									<cfloop array="#insArray#" index="dep">
                                        <cfset sql = "	SELECT
															sbj.sbj_id,sbj.sbj_name
														FROM
															t_subject sbj
														WHERE
															sbj.institute_id = :depId 
															AND
															sbj.sbj_direction = '0'
														ORDER BY
															sbj.sbj_name" />
                                                            
										<cfset queryObj = new Query(datasource = application.dnsSlave)/>
                                        <cfset queryObj.addParam(name = "depId", value = dep[1], cfsqltype = "cf_sql_varchar")/>
                                        <cfset rs_subject = queryObj.execute(sql = sql).getResult()/>
                                        
										<div id="SUB_OF_<cfoutput>#dep[1]#</cfoutput>">
											
											<cfoutput>#dep[2]#</cfoutput>学院下设专业:
											
											<table>
												<tr>
												<cfset row = 0 />
												<cfset cellPerRow = 3 />
                                                <cfloop query="rs_subject">
													<cfset row++ />
														<td>
															<input class="typeCheckbox" type="checkbox" name="targetSubject" value="<cfoutput>#rs_subject.sbj_id#</cfoutput>" <cfif listFind( rs_turnEntity.subject_allowed, rs_subject.sbj_id, ",")>checked="checked"</cfif>/>
															<cfoutput>#rs_subject.sbj_name#</cfoutput>
														</td>
													<cfif row mod cellPerRow eq 0>
												</tr>
												<tr>
													</cfif>
												</cfloop>
												<cfif row eq rs_subject.recordCount and rs_subject.recordCount mod cellPerRow>
													<cfloop from="1" to="#cellPerRow - rs_subject.recordCount mod cellPerRow#" index="i">
														<td><!-- <cfoutput>holder<cfoutput>#i#</cfoutput> --></cfoutput></td>
													</cfloop>
												</cfif>
												</tr>
											</table>

										</div>
										
									</cfloop>
								</div>

							</div>
							
							<div class="lable">
								<input class="button" type="submit" value="更新" />
								<input id="formReset" class="button1" type="reset" value="重设表格" />
							</div>
							
						</form>
						<script language="javascript" type="text/javascript">
							//<![CDATA[
							<!--
							$(document).ready(function() {
								
								$("#DateStart").mask("9999-99-99");
								$("#DateEnd").mask("9999-99-99");
								
								$("#TimeStart").mask("99:99");
								$("#TimeEnd").mask("99:99");
								
								$("#FullDay").change(function(){
									var checked = $(this).prop("checked");
									
									if ( checked ) {
										$("#TimeStart").val("00:01");
										$("#TimeEnd").val("23:59");
										$("#notFullDay").addClass("hidden_elem");
									}
									else {
										$("#notFullDay").removeClass("hidden_elem");
									}
									
								});
								
								$("#FullGrade").change(function(){
									var checked = $(this).prop("checked");
									
									if ( checked ) {
										$("#notFullGrade").addClass("hidden_elem");
									}
									else {
										$("#notFullGrade").removeClass("hidden_elem");
									}
								});
								
								$("#FullInstitute").change(function(){
									var checked = $(this).prop("checked");
									
									if ( checked ) {
										$("#notFullInstitute").addClass("hidden_elem");
									}
									else {
										$("#notFullInstitute").removeClass("hidden_elem");
										$("input[usage='targetInstitute']").attr("checked","checked");
										$("input[usage='targetInstitute']").each(function(index, element) {
											InstituteID = $(element).attr("value");
											checkedStatus = $(element).attr("checked");
											if(checkedStatus && checkedStatus=='checked'){
												//将专业置为全选
												$("#SUB_OF_" + InstituteID).addClass("hidden_elem");
											}else{
												//显示该学院的专业
												$("#SUB_OF_" + InstituteID).removeClass("hidden_elem");
											}
										});
									}
								});
								
								$("input[usage='targetInstitute']").each(function(index, element) {
									InstituteID = $(element).attr("value");
									checkedStatus = $(element).attr("checked");
									if(checkedStatus && checkedStatus=='checked'){
										//将专业置为全选
										$("#SUB_OF_" + InstituteID).addClass("hidden_elem");
									}else{
										//显示该学院的专业
										$("#SUB_OF_" + InstituteID).removeClass("hidden_elem");
									}
								});
								
								$("input[usage='targetInstitute']").click(function(e) {
									InstituteID = $(e.target).attr("value");
									checkedStatus = $(e.target).attr("checked");
									if(checkedStatus && checkedStatus=='checked'){
										//将专业置为全选
										$("#SUB_OF_" + InstituteID + " input[name='targetSubject']").removeAttr("checked");
										$("#SUB_OF_" + InstituteID).addClass("hidden_elem");
									}else{
										//将专业置为不用选中，移除checked属性
										$("#SUB_OF_" + InstituteID + " input[name='targetSubject']").removeAttr("checked");
										$("#SUB_OF_" + InstituteID).removeClass("hidden_elem");
									}
								});
								
								Calendar.setup({
									trigger    : "startTrigger",
									inputField : "DateStart",
									animation  : false
									});
								
								Calendar.setup({
									trigger    : "endTrigger",
									inputField : "DateEnd",
									animation  : false
									});

							});
							// -->
							//]]>
						</script>
						
					</div>
				</cfif>
				
				<div id="Condition" class="tabContent">
					
					<div class="noticeBlock">
						<h3>其他限制条件</h3>
						<hr/>
					</div>
					
					<form id="formEdit" onSubmit="javascript:return processVerfiyForm('formEdit');" class="formWrapper" method="post" action="<cfoutput>#buildURL('chooseSettingSaveDo')#</cfoutput>">
						
						<div class="label">
							<span class="req">*</span><b>学院补选时间段</b>
							
							<input name="DepDateStart" id="DepDateStart" type="text" class="typeDate" verification="date" maxlength="10" value="<cfoutput>#datetimeAdvice.formatDateString( depDateStart )#</cfoutput>" /><span id="depStartTrigger" class="btnCalendar"></span>
							&nbsp;&nbsp;
							至
							&nbsp;&nbsp;
							<input name="DepDateEnd" id="DepDateEnd" type="text" class="typeDate" verification="date" maxlength="10" value="<cfoutput>#datetimeAdvice.formatDateString( depDateEnd )#</cfoutput>" /><span id="depEndTrigger" class="btnCalendar"></span>
							
							<p class="notice">
								允许学院网上进行手工课程补选的时间段.
							</p>
							
						</div>
						
						<div class="lable">
							<input class="button" type="submit" value="保存" />
							<input id="formReset" class="button1" type="reset" value="重设表格" />
						</div>
						
					</form>
					
					<script language="javascript" type="text/javascript">
						//<![CDATA[
						<!--
						$(document).ready(function() {
							
							$("#DepDateStart").mask("9999-99-99");
							$("#DepDateEnd").mask("9999-99-99");
							
							Calendar.setup({
									trigger    : "depStartTrigger",
									inputField : "DepDateStart",
									animation  : false
									});
								
							Calendar.setup({
									trigger    : "depEndTrigger",
									inputField : "DepDateEnd",
									animation  : false
									});

						});
						// -->
						//]]>
					</script>
					
				</div>

			</div>
		</li>
	</ul>
</div>